#include "spi.h"
#include "gd32f10x_spi.h"
#include "gd32f10x_rcu.h"
#include "gd32f10x_gpio.h"

void SPI0_Init(void)
{
    spi_parameter_struct spi_init_struct;
    rcu_periph_clock_enable(RCU_SPI0);
    rcu_periph_clock_enable(RCU_GPIOA);

    //CS-PA4; MISO-PA6; MOSI-PA7; SCK-PA5
    gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_5|GPIO_PIN_7);
    gpio_init(GPIOA,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_6);    //浮空输入模式

    spi_i2s_deinit(SPI0);
    spi_init_struct.trans_mode =SPI_TRANSMODE_FULLDUPLEX;
    spi_init_struct.clock_polarity_phase=SPI_CK_PL_LOW_PH_1EDGE;
    spi_init_struct.device_mode=SPI_MASTER;
    spi_init_struct.endian=SPI_ENDIAN_MSB;
    spi_init_struct.frame_size=SPI_FRAMESIZE_8BIT;
    spi_init_struct.nss=SPI_NSS_SOFT;
    spi_init_struct.prescale=SPI_PSC_2;
    spi_init(SPI0,&spi_init_struct);
    spi_enable(SPI0);
}

uint8_t SPI0_ReadWritByte(uint8_t txd)
{
    while (spi_i2s_flag_get(SPI0,SPI_FLAG_TBE)!=1);
    spi_i2s_data_transmit(SPI0,txd);
    while (spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE)!=1);
    return spi_i2s_data_receive(SPI0);
}

void SPI0_Write(uint8_t* wdata,uint16_t datalen)
{
    uint16_t i;
    for(i=0;i<datalen;i++)
    {
        SPI0_ReadWritByte(wdata[i]);
    }
}

void SPI0_Read(uint8_t* rdata,uint16_t datalen)
{
    uint16_t i;
    for(i=0;i<datalen;i++)
    {
        rdata[i]=SPI0_ReadWritByte(0xff);
    }
}
